//
//  HomeCommentsController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/18.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "HomeCommentsController.h"
#import "UserIconAndNameView.h"

@interface HomeCommentsController ()<UITableViewDelegate,UITableViewDataSource>
/**<#属性#>*/
@property (nonatomic,strong)UITableView*listView;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataSoure;
/**搜索视图*/
@property (nonatomic,strong)UITextField*serachField;
/**键盘上面评论输入框*/
@property (nonatomic,strong)PubulicInputToolbar*inputToolbar;

@end

@implementation HomeCommentsController
- (void)viewDidLoad {
    [super viewDidLoad];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: self.view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15,15)];
    //创建 layer
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.view.bounds;
    //赋值
    maskLayer.path = maskPath.CGPath;
    self.view.layer.mask = maskLayer;

    [self createUI];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setIQKeyboardManagerEnable:NO];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [self setIQKeyboardManagerEnable:YES];
}
-(void)createUI{
    UIView * view = [self createHeaderView];
    
    UIView * oneLine = UIView.new;
    oneLine.backgroundColor = UIColorFromRGB(0xE9EEE7);
    
    UIView * twoLine = UIView.new;
    twoLine.backgroundColor = UIColorFromRGB(0xE9EEE7);

    [self.view addSubview:view];
    [self.view addSubview:oneLine];
    [self.view addSubview:twoLine];

    [self.view addSubview:self.listView];
    
    [oneLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(44);
    }];
    
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(oneLine.mas_bottom);
        make.bottom.mas_equalTo(-50);
    }];
    
    [twoLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(self.listView.mas_bottom);
    }];
    
    UIView * searchview = [self createSerbar];
    
    [self.view addSubview:searchview];
    
    [searchview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(twoLine.mas_bottom);
        make.left.bottom.right.mas_equalTo(0);
    }];
    [self.view addSubview:self.inputToolbar];


}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    DLogFunc;
    [self.view endEditing:YES];
}
#pragma mark ---UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeCommentsCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeCommentsCell class])];
    return cell;
}
#pragma mark ---懒加载
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-225-50) style:UITableViewStylePlain];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

        [_listView setSeparatorColor:UIColorFromRGB(0xE9EEE7)];
        [_listView registerClass:[HomeCommentsCell class] forCellReuseIdentifier:NSStringFromClass([HomeCommentsCell class])];
    }
    return _listView;
}

-(PubulicInputToolbar*)inputToolbar{
    if(!_inputToolbar){
        _inputToolbar=[[PubulicInputToolbar alloc] init];
        _inputToolbar.textViewMaxLine = 3;
        _inputToolbar.fontSize = 18;
        _inputToolbar.placeholder = @"写评论...";
        CGRect subViewFrame = [PresentationObject sharedPresentationObject].subViewFrame;
        _inputToolbar.bomttomView_y = subViewFrame.origin.y - 65;

        kWeakSelf(weakSelf);
        [_inputToolbar inputToolbarSendText:^(NSString * _Nonnull text) {
            // 清空输入框文字
            [weakSelf.inputToolbar bounceToolbar];
        }];
    }
    return _inputToolbar;
}
-(UIView *)createHeaderView{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 44)];
    UILabel * lable = UILabel.new;
    lable.textColor = UIColorFromRGB(0x273323);
    lable.font = PingFangSC_Medium(18);
    lable.text = @"232条评论";
    
    [view addSubview:lable];
    
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.bottom.mas_equalTo(0);
    }];
    
    return view;
}
-(void)tapIputAction{
    [self.inputToolbar popToolbar];
}
-(UIView * )createSerbar{
    UIView * view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    
    UIView * view1 = UIView.new;
    view1.backgroundColor = UIColorFromRGB(0xF5F7F5);
    ViewRadius(view1, 4);
    view1.userInteractionEnabled = YES;
    
    [view1 addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapIputAction)]];
    
    [view addSubview:view1];
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(38);
    }];
    
    UIImageView * leftImage = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"xiepinglun")];
    [view1 addSubview:leftImage];
    [view1 addSubview:self.serachField];
    
    
    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(8);
        make.width.height.mas_equalTo(20);
    }];
    
    [self.serachField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(leftImage.mas_right).offset(4);
        make.top.right.bottom.mas_equalTo(0);
    }];
    
    
    return view;
}

-(UITextField*)serachField{
    if(!_serachField){
        _serachField=[UITextField new];
        _serachField.placeholder = @"写评论...";
        _serachField.enabled = NO;
        
    }
    return _serachField;
}

@end

@interface HomeCommentsCell ()
/**<#属性#>*/
@property (nonatomic,strong)UserIconAndNameView*topView;

/**内容*/
@property (nonatomic,strong)UILabel*contenTex;
/**<#属性#>*/
@property (nonatomic,strong)UILabel*bottomText;
/**<#属性#>*/
@property (nonatomic,strong)UIButton * zanBtn;
@end

@implementation HomeCommentsCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)createUI{
    
    self.zanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.zanBtn setTitle:@"23" forState:UIControlStateNormal];
    [self.zanBtn setImage:IMAGE_NAMED(@"ic_like_defut") forState:UIControlStateNormal];
    [self.zanBtn setImage:IMAGE_NAMED(@"ic_like_selected1") forState:UIControlStateSelected];

    self.zanBtn.titleLabel.font =SYSTEMFONT(12);
    [self.zanBtn setTitleColor:UIColorFromRGB(0x454F44) forState:UIControlStateNormal];
    [self.zanBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 4, 0, -4)];
    
    kWeakSelf(weakSelf);
    [self.zanBtn addActionHandler:^{
        weakSelf.zanBtn.selected = !weakSelf.zanBtn.selected;
    }];
    
    [self addSubview:self.topView];
    [self addSubview:self.contenTex];
    [self addSubview:self.bottomText];
    [self.topView addSubview:self.zanBtn];

    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    [self.zanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(16);
    }];
    [self.contenTex mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(68);
        make.top.mas_equalTo(self.topView.mas_bottom);
        make.right.mas_equalTo(-16);
    }];
    
    [self.bottomText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(68);
        make.top.mas_equalTo(self.contenTex.mas_bottom).offset(8);
        make.bottom.right.mas_equalTo(-16);
        
    }];
}
-(UserIconAndNameView*)topView{
    if(!_topView){
        _topView=[[UserIconAndNameView alloc] initWithFrame:CGRectZero];
        _topView.deleButton.hidden = YES;
    }
    return _topView;
}
-(UILabel*)contenTex{
    if(!_contenTex){
        _contenTex=[UILabel new];
        _contenTex.font = SYSTEMFONT(14);
        _contenTex.textColor = UIColorFromRGB(0x273323);
        _contenTex.numberOfLines = 0;
        _contenTex.text = @"这里是我的评论，大家来点赞！";
    }
    return _contenTex;
}
-(UILabel*)bottomText{
    if(!_bottomText){
        _bottomText=[UILabel new];
        _bottomText.font = SYSTEMFONT(12);
        _bottomText.textColor = UIColorFromRGB(0x454F44);
        _bottomText.numberOfLines = 0;
        _bottomText.text = @"04-19 15:30 24回复";
    }
    return _bottomText;
}

@end
